#ifndef __TYPE_H__
#define __TYPE_H__

#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>

typedef unsigned char                   U8;
typedef unsigned short                  U16;
typedef unsigned int                    U32;
typedef char                            S8;
typedef short                           S16;
typedef int                             S32;
typedef long                            S64;

#define SUCCESS                             1
#define FAILURE                             -1

#define MAX_USER_LENGTH                     12
#define MAX_PASSWD_LENGTH                   12
#define MAX_EMAIL_LENGTH                    32
#define MONITOR_ID_NUM                      12
#define MONITOR_NAME_NUM                    32
#define MONITOR_NOTE_NUM                    32
#define TIME_NUM                            8

#define MAX_IP_NUM                          16

#define MAX_DEVICES_NUM                     10
#define MAX_APPS_NUM                        10

#define MAX_STRING_LENGTH                   128
#define MAX_BUFFER_LENGTH                   512

#define APPS_PORT                           9999

#define PASSWD_FILE                         "passwd.txt"
#define MONITOR_FILE                        "monitor_information.txt"

enum {
    DEVICE_TYPE_HOUSE = 1,
    DEVICE_TYPE_ILLEGAL
} DEVICE_TYPE;

enum {
    CMD_TYPE_LOGIN = 0x1,
    CMD_TYPE_CHANGE_PASSWORD = 0x2,
    CMD_TYPE_RESET_PASSWORD = 0x3,
    CMD_TYPE_GET_ALL_MONITOR = 0x1001,
    CMD_TYPE_GET_MONITOR_DATA = 0x1002,
    CMD_TYPE_GET_HISTORY_DATA = 0x1003,
    CMD_TYPE_SET_WATER_VALVE = 0x1004,
    CMD_TYPE_GET_PLAN = 0x2001,
    CMD_TYPE_SET_PLAN = 0x2002,
    CMD_TYPE_ILLEGAL
} COMMAND_TYPE;

typedef struct {
    U32 head_flag;
    U16 cmd_type;
    U8 response;
    U16 msg_length;
} __attribute__ ((packed)) msg_header_t;

typedef struct {
    S32 fd;
    struct sockaddr_in cin;
} msg_channel_t;

typedef struct {
    U8 water_valve_control;
    U8 time_type;
    U8 time_start[TIME_NUM];
    U8 time_hold[TIME_NUM];
} __attribute__ ((packed)) time_plan_t;

typedef struct {
    U8 water_valve_control;
    U8 time_hold[TIME_NUM];
    U8 temperature_vaild;
    U8 temperature_relation;
    U16 temperature_threshold;
    U8 humidity_vaild;
    U8 humidity_relation;
    U16 humidity_threshold;
    U8 co2_vaild;
    U8 co2_relation;
    U16 co2_threshold;
    U8 luminance_vaild;
    U8 luminance_relation;
    U16 luminance_threshold;
    U8 soil_humidity_vaild;
    U8 soil_humidity_relation;
    U16 soil_humidity_threshold;
} __attribute__ ((packed)) threshold_plan_t;

typedef struct {
    U8 type;
    time_plan_t timePlan;
    threshold_plan_t thresholdPlan;
} __attribute__ ((packed)) plan_t;

#endif
